const Koa = require('koa')
const app = new Koa()
const { connect } = require('./src/schema/index')
const registerRoutes = require('./src/routes/index')
const koaBody = require('koa-body')
const cors = require('koa2-cors') //TODO: 解决跨域问题
const { checker } = require('./src/utils/token')
const { catchTokenError } = require('./src/utils/token')
const { middleware: koaJwtMiddleware } = require('./src/utils/token')
const { logMiddleware } = require('./src/middleware/logger')
const koaStatic = require('koa-static')
const path = require('path')
const { SERVER_PORT } = require('./src/project.config')

app.use(koaStatic(path.resolve(__dirname, './public')))

connect().then(() => {
    app.use(cors())
    app.use(koaBody())
    app.use(catchTokenError) //todo: 这里catchTokenError后面不加括号，比较特殊
    koaJwtMiddleware(app)
    app.use(checker)
    app.use(logMiddleware)
    registerRoutes(app)
    app.listen(SERVER_PORT, () => {
        console.log(`启动成功，listening on http://localhost:${SERVER_PORT}`)
    })
})

